package cn.zebra.dev.product.module.attribute.service.impl;

import cn.zebra.dev.common.utils.DateUtils;
import cn.zebra.dev.common.utils.StrUtils;
import cn.zebra.dev.ibatis.plus.service.impl.ServiceImpl;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.ibatis.plus.utils.Query;
import cn.zebra.dev.product.module.attribute.dao.AttributeDao;
import cn.zebra.dev.product.module.attribute.entity.AttributeEntity;
import cn.zebra.dev.product.module.attribute.service.AttributeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author runnable@sina.cn
 */
@Service
public class AttributeServiceImpl extends ServiceImpl<AttributeDao, AttributeEntity> implements AttributeService {
    @Override
    public PageUtils<AttributeEntity> findPage(Map<String, Object> params) {
        QueryWrapper<AttributeEntity> wrapper = new QueryWrapper<>();
        wrapper.select("id, product_type_id, attribute_name, input_flag, attribute_list, enable_flag");
        wrapper.apply(params.containsKey("attributeName"), "attribute_name LIKE CONCAT('%', {0}, '%') ", params.get("attributeName"));
        wrapper.apply("delete_flag = FALSE");
        wrapper.orderByDesc("id");
        IPage<AttributeEntity> page = page(new Query<AttributeEntity>(params).getPage(), wrapper);
        return new PageUtils<>(page);
    }

    @Override
    public Boolean saveAttribute(AttributeEntity entity, Long createId) {
        if (StrUtils.isBlank(entity.getAttributeList())) {
            entity.setAttributeList("");
        }
        entity.setCreateId(createId);
        entity.setCreateTime(LocalDateTime.now());
        entity.setUpdateId(0L);
        entity.setUpdateTime(DateUtils.defaultTime());
        entity.setDeleteFlag(false);
        return save(entity);
    }

    @Override
    public Boolean updateAttribute(Long id, AttributeEntity entity, Long updateId) {
        if (StrUtils.isBlank(entity.getAttributeList())) {
            entity.setAttributeList("");
        }
        entity.setCreateId(null);
        entity.setCreateTime(null);
        entity.setUpdateId(updateId);
        entity.setUpdateTime(LocalDateTime.now());
        entity.setDeleteFlag(false);
        return update(entity, "id = {0}", id);
    }

    @Override
    public AttributeEntity findById(Long id) {
        QueryWrapper<AttributeEntity> wrapper = new QueryWrapper<>();
        wrapper.select("id, attribute_name, input_flag, attribute_list, enable_flag");
        wrapper.apply("id = {0}", id);
        wrapper.apply("delete_flag = FALSE");
        wrapper.orderByDesc("id");
        return getOne(wrapper);
    }

    @Override
    public List<AttributeEntity> findByIds(Collection<Long> idList) {
        if (idList == null || idList.size() == 0) {
            return new ArrayList<>();
        }
        QueryWrapper<AttributeEntity> wrapper = new QueryWrapper<>();
        wrapper.in("id", idList);
        return this.list(wrapper);
    }
}
